﻿
using Game.Global.Helper;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace Game
{
    /// <summary>
    /// 用于帮助指向Checkpoint的位置并使用WorldCanvas显示
    /// </summary>
    public class CheckPointDirectionHelper : MonoBehaviour
    {
        static CheckPointDirectionHelper instance;
        List<GameObject> m_teachPoints = new List<GameObject>();
        Transform m_transform;
        Transform m_targetTrans;

        Canvas m_canvas;

        [SerializeField]
        bool m_showUIDirection = false;
        [SerializeField]
        float m_imgColorTweenSpeed=2;

        private void Awake()
        {
            //if (instance == null)
            //{
                instance = this;

            //}
        }

        private void Start()
        {

            CheckPointBase.AddOnTeachPointShowedUp(OnTeachPointShowedUp);

            CheckPointBase.AddOnTeachPointShowedUp(OnTeachPointTriggered);

            GameControllerBase.AddOnGameOver((isOver) => { TurnUITo(false); });         //对于游戏结束时，不管gameOver与否，都要关闭此UI显示

            InitUI();

            m_transform = transform;
        }


        void Update()
        {
            if(m_showUIDirection)
            {
                //更新UI效果
                UpdateUI();

                //更新物体转向，过近时则关闭：
                UpdateTrans();

                

            }
        }

        private void UpdateTrans()
        {
            //距离够近时，关闭UI提示：
            if(m_transform.SqrDistanceTo(m_targetTrans) < 300)//10米内
            {
                TurnUITo(false);
                return;
            }

            m_transform.LookAt(m_targetTrans);

        }

        #region Public part
        #endregion


        #region Private part

        private void OnTeachPointShowedUp(GameObject teachPoint)
        {
            Debug.Log("Showed up :" + teachPoint.name);
            m_teachPoints.Add(teachPoint);

            //准备Work？
            CheckUIWork(teachPoint);
        }


        private void OnTeachPointTriggered(GameObject param)
        {
            
        }

        private void CheckUIWork(GameObject specifyTarget=null)
        {
            if (GameObjFunc.IsNullOrEmpty(m_teachPoints))
            {
                TurnUITo(false);
                return;
            }

            bool hasTarget = false;

            if (specifyTarget)
            {
                hasTarget = true;
                m_targetTrans = specifyTarget.transform;
            }
            else
            {
                //找出一个可用的目标点：
                foreach (var point in m_teachPoints)
                {
                    if (point != null && point.activeInHierarchy)
                    {
                        hasTarget = true;
                        m_targetTrans = point.transform;
                        break;
                    }
                }
            }

            TurnUITo(hasTarget);
        }

        #region UI part
        Image m_arrowImage;
        float m_imgAlpha;
        private void InitUI()
        {
            m_canvas = this.GetComponentInChildren<Canvas>();
            //获取Image组件：
            m_arrowImage = m_canvas.GetComponentInChildren<Image>();

            m_imgAlpha = m_arrowImage.color.a;

            //一开始无需打开：
            TurnUITo(false);
        }


        float timePassed = 0;
        Color tmpImageColor;
        private void UpdateUI()
        {
            //渐变颜色的透明度：
            timePassed += Time.deltaTime *m_imgColorTweenSpeed;
            tmpImageColor = m_arrowImage.color;
            tmpImageColor.a = Mathf.Clamp( m_imgAlpha * Mathf.Sin(timePassed) + 0.5f/*Y上偏移*/,0,m_imgAlpha);
            m_arrowImage.color = tmpImageColor;
        }

        private void TurnUITo(bool isOn)
        {
            if (!isOn)
            {
                m_targetTrans = null;//清空之前的结果：
                m_showUIDirection = false;
            }
            else
            {
                m_showUIDirection = true;
            }
            if (m_canvas!=null )
            {
                m_canvas.gameObject.SetActive(isOn);
            }
        }
        #endregion

        #endregion
    }

}